#include <QtGui>
#include <QToolButton>
#include <QFile>
#include <string>
//#include <cassert>

#include "gomainwindow.h"
#include "board.h"
#include "settings.h"

GoMainWindow::~GoMainWindow()
{
    if(!gameSettings) delete gameSettings;
}

GoMainWindow::GoMainWindow()
{
    setWindowTitle(tr("GoGame"));
    setWindowIcon(QIcon(":/images/go.png")); //this Image is copied from a OnlineGame Center called "QQGame"

    boardView = new Board();
    //boardView->setFixedSize(610,610);
    boardView->show();
    setCentralWidget(boardView);

    gameSettings = new Settings();
    createActions();
    createMenus();
    createToolButtons();
    createToolBars();
    createStatusBar();

    connect(boardView, SIGNAL(changeButtonToFalse()), this, SLOT(falseButtonDisplay()));
    connect(boardView, SIGNAL(openReview()), this, SLOT(openReview()));
    connect(boardView, SIGNAL(closeReview()), this, SLOT(closeReview()));
    connect(boardView, SIGNAL(disableShowIndex()), this, SLOT(disableShowIndex()));
    connect(boardView, SIGNAL(enableShowIndex()), this, SLOT(enableShowIndex()));
    connect(boardView, SIGNAL(disableGameSettings()), this, SLOT(disableGameSettings()));
    connect(boardView, SIGNAL(enableGameSettings()), this, SLOT(enableGameSettings()));
    connect(boardView, SIGNAL(windowIsModified()), this, SLOT(windowIsModified()));
    connect(boardView, SIGNAL(loadSucceed()), this, SLOT(loadSucceed()));
    connect(boardView, SIGNAL(loadFailed()), this, SLOT(loadFailed()));

    /** game IO signals **/
    connect(this, SIGNAL(outputSGF(QFile*, Settings*)), boardView, SLOT(outputSGF(QFile*, Settings*)));
    connect(this, SIGNAL(inputSGF(QFile*, Settings*)), boardView, SLOT(inputSGF(QFile*, Settings*)));
    connect(this, SIGNAL(output2DBoard(const char*)), boardView, SLOT(output2DBoard(const char*)));
    /** game settings signals **/
    connect(this, SIGNAL(changeHandicaps(int)), boardView, SLOT(changeHandicaps(int)));

            startReviewButton->setEnabled(false);
            backToFirstButton->setEnabled(false);
            jumpBackwardButton->setEnabled(false);
            backwardButton->setEnabled(false);
            forwardButton->setEnabled(false);
            jumpForwardButton->setEnabled(false);
            goToEndButton->setEnabled(false);
            backToInitButton->setEnabled(false);
            quitReviewButton->setEnabled(false);
            regretButton->setEnabled(true);
}

void GoMainWindow::newGame()
{
    if(okToContinue())
    {
        if(!gameSettings)
        {
            delete gameSettings;
        }
        gameSettings = new Settings();
        curFile.clear(); //新游戏文件名清零
        setWindowModified(false);

        boardView->newGame();
            startReviewButton->setEnabled(false);
            backToFirstButton->setEnabled(false);
            jumpBackwardButton->setEnabled(false);
            backwardButton->setEnabled(false);
            forwardButton->setEnabled(false);
            jumpForwardButton->setEnabled(false);
            goToEndButton->setEnabled(false);
            backToInitButton->setEnabled(false);
            quitReviewButton->setEnabled(false);
            regretButton->setEnabled(true);
            passButton->setEnabled(true);
            showIndexAction->setEnabled(true);
        if(showGridAction->isChecked()) //keep this setting not changed by restart a newgame
        {
            boardView->setShowGrid(true);
        }
    }
    else return;
}

void GoMainWindow::beforeClose()
{
    if(okToContinue()) close();
    else return;
}

void GoMainWindow::windowIsModified()
{
    setWindowModified(true);
}

bool GoMainWindow::okToContinue()
{
    if (isWindowModified()) {
        int r = QMessageBox::warning(this, tr("GoGame"),
                        tr("The document has been modified.\n"
                           "Do you want to save your changes?"),
                        QMessageBox::Yes | QMessageBox::No
                        | QMessageBox::Cancel);
        if (r == QMessageBox::No) setWindowModified(false);
        else if (r == QMessageBox::Yes) {
            return save();
        } else if (r == QMessageBox::Cancel) {
            return false;
        }
    }
    return true;
}

bool GoMainWindow::save()
{
    if (curFile.isEmpty()) {
        return saveAs();
    }
    else {
        if (isWindowModified()) {
            int r = QMessageBox::warning(this, tr("GoGame"),
                            tr("The document is already exist.\n"
                               "Do you want to overwrite it?"),
                            QMessageBox::Yes | QMessageBox::No
                            | QMessageBox::Cancel);
            if (r == QMessageBox::No) return saveAs();
            else if (r == QMessageBox::Yes) {
                return saveGame(curFile);
            } else if (r == QMessageBox::Cancel) {
                return false;
              }
        }
    }
    return saveGame(curFile);
}

bool GoMainWindow::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this,
                               tr("Save SGF GoGame"), ".",
                               tr("SmartGameFormat Go files (*.sgf)"));
    if (fileName.isEmpty())
        return false;
    else
    {
        curFile = fileName;
        return saveGame(fileName); //dont have to check if fileName already exist,
                                   //QFileDialog helped out
    }
}

void GoMainWindow::save2DBoard()
{
    QString fileName = QFileDialog::getSaveFileName(this,
                               tr("Output 2D-Board-Array as binary File"), ".",
                               tr("Binary Files (*.bin)"));
    if (fileName.isEmpty())
        return;
    else
    {
        const std::string sfileName = fileName.toStdString();
        const char* cfileName = sfileName.c_str();
        emit output2DBoard(cfileName);
    }
}

void GoMainWindow::open()
{
    if (okToContinue()) {
        QString fileName = QFileDialog::getOpenFileName(this,
                                   tr("Open SGF GoGame"), ".",
                                   tr("SmartGameFormat Go files (*.sgf)"));
        if (!fileName.isEmpty())
            loadGame(fileName);
    }
}

void GoMainWindow::loadSucceed()
{
    QMessageBox::information(this, tr("LoadGame"), tr("Loading successfully!"));
}

void GoMainWindow::loadFailed()
{
    QMessageBox::information(this, tr("LoadGame"), tr("Loading failed!\nDue to wrong syntax or content of sgf File."));
    newGame(); //初始化所有数据
}

bool GoMainWindow::loadGame(const QString &filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly)) {
            QMessageBox::warning(this, QObject::tr("Gogame"),
                            QObject::tr("Cannot open file %1:\n%2.")
                            .arg(file.fileName())
                            .arg(file.errorString()));
        statusBar()->showMessage(tr("File open failed"), 2000);
        return false;
    }

    else
    {
        newGame(); //保证全部数据为初始状态, 因为已经询问过保存与否
        curFile = filename;
        statusBar()->showMessage(tr("File opened successfully"), 2000);
        emit inputSGF(&file, gameSettings);
        return true;
    }
}

bool GoMainWindow::saveGame(const QString &filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::WriteOnly)) {
            QMessageBox::warning(this, QObject::tr("Gogame"),
                            QObject::tr("Cannot write file %1:\n%2.")
                            .arg(file.fileName())
                            .arg(file.errorString()));
        statusBar()->showMessage(tr("Game saving failed"), 2000);
        return false;
    }

    else
    {
        emit outputSGF(&file, gameSettings);
        statusBar()->showMessage(tr("Game saved successfully"), 2000);
        setWindowModified(false);
        QMessageBox::information(this, tr("SaveGame"), tr("Saving successfully!"));
        return true;
    }
}

void GoMainWindow::about()
{
    QMessageBox::about(this, tr("About GoGame"),
            tr("<h2>GoGame 2.0</h2>"
               "<p>Marsowner's Game ^^"
               "<p>GoGame is a small application that "
               "people can play Go on it."));
}

void GoMainWindow::trueButtonDisplay()
{
    backToFirstButton->setEnabled(true);
    jumpBackwardButton->setEnabled(true);
    jumpForwardButton->setEnabled(true);
    goToEndButton->setEnabled(true);
}

void GoMainWindow::falseButtonDisplay()
{
    backToFirstButton->setEnabled(false);
    jumpBackwardButton->setEnabled(false);
    jumpForwardButton->setEnabled(false);
    goToEndButton->setEnabled(false);
}

void GoMainWindow::openReview()
{
    startReviewButton->setEnabled(true);
}

void GoMainWindow::closeReview()
{
    startReviewButton->setEnabled(false);
}

void GoMainWindow::disableGameSettings()
{
    gameSettingsAction->setEnabled(false);
}

void GoMainWindow::enableGameSettings()
{
    gameSettingsAction->setEnabled(true);
}

void GoMainWindow::disableShowIndex()
{
    showIndexAction->setEnabled(false);
}

void GoMainWindow::enableShowIndex()
{
    showIndexAction->setEnabled(true);
}

void GoMainWindow::createActions()
{
    newAction = new QAction(tr("&New"), this);
    newAction->setIcon(QIcon(":/images/new.png"));
    newAction->setShortcut(QKeySequence::New);
    newAction->setStatusTip(tr("Restart a Newgame"));
    connect(newAction, SIGNAL(triggered()), this, SLOT(newGame()));

    saveAction = new QAction(tr("&Save"), this);
    saveAction->setIcon(QIcon(":/images/save.png"));
    saveAction->setStatusTip(tr("Save current Game"));
    connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));

    saveAsAction = new QAction(tr("Save&As..."), this);
    saveAsAction->setStatusTip(tr("Save current Game As"));
    connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));

    openAction = new QAction(tr("&Load"), this);
    openAction->setIcon(QIcon(":/images/open.png"));
    openAction->setStatusTip(tr("Load a Game"));
    connect(openAction, SIGNAL(triggered()), this, SLOT(open()));

    output2DBoardAction = new QAction(tr("Output 2D bin-Board..."), this);
    output2DBoardAction->setStatusTip(tr("Output 2D-Board as a binary File"));
    connect(output2DBoardAction, SIGNAL(triggered()), this, SLOT(save2DBoard()));

    exitAction = new QAction(tr("E&xit"), this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("Quit the GoGame"));
    connect(exitAction, SIGNAL(triggered()), this, SLOT(beforeClose()));

    aboutAction = new QAction(tr("&About"), this);
    aboutAction->setShortcut(tr("Ctrl+A"));
    aboutAction->setStatusTip(tr("About GoGame"));
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));

    showGridAction = new QAction(tr("Show &Grid"), this);
    showGridAction->setCheckable(true);
    //showGridAction->setChecked(boardView->setShowGrid(true));
    showGridAction->setStatusTip(tr("Show or hide the goboard's "
                                    "grid"));
    connect(showGridAction, SIGNAL(toggled(bool)),
            boardView, SLOT(setShowGrid(bool)));

    showIndexAction = new QAction(tr("Show &Index"), this);
    showIndexAction->setCheckable(true);
    showIndexAction->setStatusTip(tr("Show or hide the index series"));
    connect(showIndexAction, SIGNAL(toggled(bool)), boardView, SLOT(setShowIndex(bool)));

    gameSettingsAction = new QAction(tr("Game &Settings"), this);
    gameSettingsAction->setStatusTip(tr("change settings for a new game"));
    connect(gameSettingsAction, SIGNAL(triggered()), this, SLOT(openGameSettingsDialog()));

    startReviewAction = new QAction(tr("Start Review"), this);
    startReviewAction->setIcon(QIcon(":/images/quit.png"));
    startReviewAction->setStatusTip(tr("Start Review"));
    connect(startReviewAction, SIGNAL(triggered()), boardView, SLOT(startReview()));
    connect(startReviewAction, SIGNAL(triggered()), this, SLOT(startReview()));

    backToFirstAction = new QAction(tr("Back To Zero Move"), this);
    backToFirstAction->setIcon(QIcon(":/images/backtofirst.png"));
    backToFirstAction->setStatusTip(tr("Back To Zero Move"));
    connect(backToFirstAction, SIGNAL(triggered()), boardView, SLOT(backToFirst()));

    jumpBackwardAction = new QAction(tr("Jump Back 10 Moves"), this);
    jumpBackwardAction->setIcon(QIcon(":/images/jumpbackward.png"));
    jumpBackwardAction->setStatusTip(tr("Jump Back 10 Moves"));
    connect(jumpBackwardAction, SIGNAL(triggered()), boardView, SLOT(jumpBackward()));

    backwardAction = new QAction(tr("One Move Backward"), this);
    backwardAction->setIcon(QIcon(":/images/backward.png"));
    backwardAction->setStatusTip(tr("One Move Backward"));
    connect(backwardAction, SIGNAL(triggered()), boardView, SLOT(backward()));

    forwardAction = new QAction(tr("One Move Forward"), this);
    forwardAction->setIcon(QIcon(":/images/forward.png"));
    forwardAction->setStatusTip(tr("One Move Forward"));
    connect(forwardAction, SIGNAL(triggered()), boardView, SLOT(forward()));

    jumpForwardAction = new QAction(tr("Jump Ahead 10 Moves"), this);
    jumpForwardAction->setIcon(QIcon(":/images/jumpforward.png"));
    jumpForwardAction->setStatusTip(tr("Jump Ahead 10 Moves"));
    connect(jumpForwardAction, SIGNAL(triggered()), boardView, SLOT(jumpForward()));

    goToEndAction = new QAction(tr("Go To Last Move"), this);
    goToEndAction->setIcon(QIcon(":/images/gotoend.png"));
    goToEndAction->setStatusTip(tr("Go To Last Move"));
    connect(goToEndAction, SIGNAL(triggered()), boardView, SLOT(goToEnd()));

    backToInitAction = new QAction(tr("Back To Change Point"), this);
    backToInitAction->setIcon(QIcon(":/images/init.png"));
    backToInitAction->setStatusTip(tr("Back To Change Point"));
    connect(backToInitAction, SIGNAL(triggered()), boardView, SLOT(backToInit()));
    connect(backToInitAction, SIGNAL(triggered()), this, SLOT(trueButtonDisplay()));

    quitReviewAction = new QAction(tr("Quit Review"), this);
    quitReviewAction->setIcon(QIcon(":/images/stop.png"));
    quitReviewAction->setStatusTip(tr("Quit Review"));
    connect(quitReviewAction, SIGNAL(triggered()), boardView, SLOT(quitReview()));
    connect(quitReviewAction, SIGNAL(triggered()), this, SLOT(quitReview()));

    regretAction = new QAction(tr("Regret Last Move"), this);
    regretAction->setIcon(QIcon(":/images/regret.png"));
    regretAction->setStatusTip(tr("Regret Last Move"));
    connect(regretAction, SIGNAL(triggered()), boardView, SLOT(regret()));

    judgmentAction = new QAction(tr("CHN Rule Judgment"), this);
    judgmentAction->setIcon(QIcon(":/images/question_mark.png"));
    judgmentAction->setStatusTip(tr("CHN Rule Judgment"));
    connect(judgmentAction, SIGNAL(triggered()), boardView, SLOT(startJudgment()));

    passAction = new QAction(tr("PASS this move"), this);
    passAction->setIcon(QIcon(":/images/pass.png"));
    passAction->setStatusTip(tr("PASS this move"));
    connect(passAction, SIGNAL(triggered()), boardView, SLOT(pass()));

    aiMoveAction = new QAction(tr("Use AI to generate a Move"), this);
    aiMoveAction->setIcon(QIcon(":/images/aimove.png"));
    aiMoveAction->setStatusTip(tr("Use AI to generate a Move"));
    connect(aiMoveAction, SIGNAL(triggered()), boardView, SLOT(aiMove()));
}

void GoMainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&Files"));
    fileMenu->addAction(newAction);
    fileMenu->addAction(openAction);
    fileMenu->addAction(saveAction);
    fileMenu->addAction(saveAsAction);
    fileMenu->addSeparator();
    fileMenu->addAction(output2DBoardAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);

    optionMenu = menuBar()->addMenu(tr("&Options"));
    optionMenu->addAction(showGridAction);
    optionMenu->addAction(showIndexAction);

    settingMenu = menuBar()->addMenu(tr("&Settings"));
    settingMenu->addAction(gameSettingsAction);

    helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(aboutAction);
}

void GoMainWindow::createToolBars()
{
    fileToolBar = addToolBar(tr("&File"));
    fileToolBar->addAction(newAction);
    fileToolBar->addAction(openAction);
    fileToolBar->addAction(saveAction);

    editToolBar = addToolBar(tr("Review Control"));
    editToolBar->insertWidget(startReviewAction, startReviewButton);
    editToolBar->insertSeparator(startReviewAction);
    editToolBar->insertWidget(backToFirstAction, backToFirstButton);
    editToolBar->insertWidget(jumpBackwardAction, jumpBackwardButton);
    editToolBar->insertWidget(backwardAction, backwardButton);
    editToolBar->insertWidget(forwardAction, forwardButton);
    editToolBar->insertWidget(jumpForwardAction, jumpForwardButton);
    editToolBar->insertWidget(goToEndAction, goToEndButton);
    editToolBar->insertSeparator(goToEndAction);
    editToolBar->insertWidget(backToInitAction, backToInitButton);
    editToolBar->insertWidget(quitReviewAction, quitReviewButton);
    editToolBar->insertSeparator(quitReviewAction);
    editToolBar->insertWidget(regretAction, regretButton);
    editToolBar->insertWidget(passAction, passButton);
    editToolBar->insertSeparator(passAction);
    editToolBar->insertWidget(judgmentAction, judgmentButton);
    editToolBar->insertWidget(aiMoveAction, aiMoveButton);
}

void GoMainWindow::createToolButtons()
{
    startReviewButton = new QToolButton();
    startReviewButton->setIcon(QIcon(":/images/quit.png"));
    startReviewButton->setStatusTip(tr("Start Review"));
    startReviewButton->setToolTip(tr("Start Review"));
    startReviewButton->setDefaultAction(startReviewAction);

    backToFirstButton = new QToolButton();
    backToFirstButton->setIcon(QIcon(":/images/backtofirst.png"));
    backToFirstButton->setStatusTip(tr("Back To First Move"));
    backToFirstButton->setToolTip(tr("Back To First Move"));
    backToFirstButton->setDefaultAction(backToFirstAction);

    jumpBackwardButton = new QToolButton();
    jumpBackwardButton->setIcon(QIcon(":/images/jumpbackward.png"));
    jumpBackwardButton->setStatusTip(tr("Jump Back 10 Moves"));
    jumpBackwardButton->setToolTip(tr("Jump Back 10 Moves"));
    jumpBackwardButton->setDefaultAction(jumpBackwardAction);

    backwardButton = new QToolButton();
    backwardButton->setIcon(QIcon(":/images/backward.png"));
    backwardButton->setStatusTip(tr("One Move Backward"));
    backwardButton->setToolTip(tr("One Move Backward"));
    backwardButton->setDefaultAction(backwardAction);

    forwardButton = new QToolButton();
    forwardButton->setIcon(QIcon(":/images/forward.png"));
    forwardButton->setStatusTip(tr("One Move Forward"));
    forwardButton->setToolTip(tr("One Move Forward"));
    forwardButton->setDefaultAction(forwardAction);

    jumpForwardButton = new QToolButton();
    jumpForwardButton->setIcon(QIcon(":/images/jumpforward.png"));
    jumpForwardButton->setStatusTip(tr("Jump Ahead 10 Moves"));
    jumpForwardButton->setToolTip(tr("Jump Ahead 10 Moves"));
    jumpForwardButton->setDefaultAction(jumpForwardAction);

    goToEndButton = new QToolButton();
    goToEndButton->setIcon(QIcon(":/images/gotoend.png"));
    goToEndButton->setStatusTip(tr("Go To Last Move"));
    goToEndButton->setToolTip(tr("Go To Last Move"));
    goToEndButton->setDefaultAction(goToEndAction);

    backToInitButton = new QToolButton();
    backToInitButton->setIcon(QIcon(":/images/init.png"));
    backToInitButton->setStatusTip(tr("Back To Init Point"));
    backToInitButton->setToolTip(tr("Back To Init Point"));
    backToInitButton->setDefaultAction(backToInitAction);

    quitReviewButton = new QToolButton();
    quitReviewButton->setIcon(QIcon(":/images/stop.png"));
    quitReviewButton->setStatusTip(tr("Quit Review"));
    quitReviewButton->setToolTip(tr("Quit Review"));
    quitReviewButton->setDefaultAction(quitReviewAction);

    regretButton = new QToolButton();
    regretButton->setIcon(QIcon(":/images/regret.png"));
    regretButton->setStatusTip(tr("Regret Last Move"));
    regretButton->setToolTip(tr("Regret Last Move"));
    regretButton->setDefaultAction(regretAction);

    judgmentButton = new QToolButton();
    judgmentButton->setIcon(QIcon(":/images/question_mark.png"));
    judgmentButton->setStatusTip(tr("CHN Rule Judgment"));
    judgmentButton->setToolTip(tr("CHN Rule Judgment"));
    judgmentButton->setDefaultAction(judgmentAction);

    passButton = new QToolButton();
    passButton->setIcon(QIcon(":/images/pass.png"));
    passButton->setStatusTip(tr("PASS this move"));
    passButton->setToolTip(tr("PASS this move"));
    passButton->setDefaultAction(passAction);

    aiMoveButton = new QToolButton();
    aiMoveButton->setIcon(QIcon(":/images/aimove.png"));
    aiMoveButton->setStatusTip(tr("Use AI to generate a Move"));
    aiMoveButton->setToolTip(tr("Use AI to generate a Move"));
    aiMoveButton->setDefaultAction(aiMoveAction);
}

void GoMainWindow::createStatusBar()
{
    statusLabel = new QLabel(tr("Status:"));

    statusBar()->addWidget(statusLabel);
}

void GoMainWindow::changeStatus(int i)
{
    status = i;

    switch (status)
    {
        case 1: // 1 -> normal
            startReviewButton->setEnabled(true);
            backToFirstButton->setEnabled(false);
            jumpBackwardButton->setEnabled(false);
            backwardButton->setEnabled(false);
            forwardButton->setEnabled(false);
            jumpForwardButton->setEnabled(false);
            goToEndButton->setEnabled(false);
            backToInitButton->setEnabled(false);
            quitReviewButton->setEnabled(false);
            regretButton->setEnabled(true);
            passButton->setEnabled(true);
            newAction->setEnabled(true);
            saveAction->setEnabled(true);
            openAction->setEnabled(true);

            break;

        case 2: // 2 -> after push startReview(init & startReview disable)
            startReviewButton->setEnabled(false);
            backToFirstButton->setEnabled(true);
            jumpBackwardButton->setEnabled(true);
            backwardButton->setEnabled(true);
            forwardButton->setEnabled(true);
            jumpForwardButton->setEnabled(true);
            goToEndButton->setEnabled(true);
            backToInitButton->setEnabled(true);
            quitReviewButton->setEnabled(true);
            regretButton->setEnabled(false);
            passButton->setEnabled(false);
            newAction->setEnabled(false);
            saveAction->setEnabled(false);
            openAction->setEnabled(false);

            break;

        /*case 3: // 3 -> init enabled
            backToInitButton->setEnabled(true);

            break;

        case 4: // 4 -> backward disabled
            backToFirstButton->setEnabled(false);
            jumpBackwardButton->setEnabled(false);
            backwardButton->setEnabled(false);

            break;

        case 5: // 5 -> forward disabled
            forwardButton->setEnabled(false);
            jumpForwardButton->setEnabled(false);
            goToEndButton->setEnabled(false);

            break;

        case 6: // 6 -> backward enabled
            backToFirstButton->setEnabled(true);
            jumpBackwardButton->setEnabled(true);
            backwardButton->setEnabled(true);

            break;

        case 7: // 7 -> forward enabled
            forwardButton->setEnabled(true);
            jumpForwardButton->setEnabled(true);
            goToEndButton->setEnabled(true);

        case 8: // 8 -> init disabled
            backToInitButton->setEnabled(false);

            break;*/

        default: break;
    }
}

void GoMainWindow::startReview()
{
    changeStatus(2);
}

void GoMainWindow::quitReview()
{
    changeStatus(1);
}

void GoMainWindow::openGameSettingsDialog()
{
    QDialog *gameSettingsDialog = new QDialog;
    gameSettingsDialog->setWindowTitle(tr("Game Settings"));

    QVBoxLayout *settingsLayout = new QVBoxLayout;
    QVBoxLayout *groupboxLayout = new QVBoxLayout;
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    QVBoxLayout *handicapsLayout = new QVBoxLayout;
    QPushButton *yesButton = new QPushButton(tr("Apply"));
    connect(yesButton, SIGNAL(clicked()), gameSettingsDialog, SLOT(accept()));
    connect(yesButton, SIGNAL(clicked()), this, SLOT(gs_exec()));
    QPushButton *noButton = new QPushButton(tr("Cancel"));
    connect(noButton, SIGNAL(clicked()), gameSettingsDialog, SLOT(reject()));
    QGroupBox *handicapsGroupBox = new QGroupBox(tr("Handicaps"));
    QGroupBox *komiGroupBox = new QGroupBox(tr("Komi"));
    QGroupBox *timeGroupBox = new QGroupBox(tr("Timing"));

    handicapsComboBox = new QComboBox;
    handicapsComboBox->insertItem(0, tr("None"));
    handicapsComboBox->insertItem(1, "2");
    handicapsComboBox->insertItem(2, "3");
    handicapsComboBox->insertItem(3, "4");
    handicapsComboBox->insertItem(4, "5");
    handicapsComboBox->insertItem(5, "6");
    handicapsComboBox->insertItem(6, "7");
    handicapsComboBox->insertItem(7, "8");
    handicapsComboBox->insertItem(8, "9");

    handicapsLayout->addWidget(handicapsComboBox);
    handicapsGroupBox->setLayout(handicapsLayout);

    buttonLayout->addWidget(yesButton);
    buttonLayout->addWidget(noButton);

    groupboxLayout->addWidget(handicapsGroupBox);
    groupboxLayout->addWidget(komiGroupBox);
    groupboxLayout->addWidget(timeGroupBox);

    settingsLayout->addLayout(groupboxLayout);
    settingsLayout->addLayout(buttonLayout);
    gameSettingsDialog->setLayout(settingsLayout);

    gameSettingsDialog->exec(); //use exec() to run this as a modal dialog
                                //A modal dialog is a dialog that blocks input to other visible windows in the same application
}

void GoMainWindow::gs_exec()
{
    //从设置界面中读取参数放入Settings类对象gameSetting中
    gameSettings->write_HA(handicapsComboBox->currentIndex() + 1);
    switch (gameSettings->get_HA())
    {
        case 2:
        {
            gameSettings->write_AB(4,4); gameSettings->write_AB(16,16);
            break;
        }
        case 3:
        {
            gameSettings->write_AB(4,4); gameSettings->write_AB(16,16); gameSettings->write_AB(4,16);
            break;
        }
        case 4:
        {
            gameSettings->write_AB(4,4); gameSettings->write_AB(16,16); gameSettings->write_AB(4,16); gameSettings->write_AB(16,4);
            break;
        }
        case 5:
        {
            gameSettings->write_AB(4,4); gameSettings->write_AB(16,16); gameSettings->write_AB(4,16); gameSettings->write_AB(16,4);
            gameSettings->write_AB(10,10);
            break;
        }
        case 6:
        {
            gameSettings->write_AB(4,4); gameSettings->write_AB(16,16); gameSettings->write_AB(4,16); gameSettings->write_AB(16,4);
            gameSettings->write_AB(10,4); gameSettings->write_AB(10,16);
            break;
        }
        case 7:
        {
            gameSettings->write_AB(4,4); gameSettings->write_AB(16,16); gameSettings->write_AB(4,16); gameSettings->write_AB(16,4);
            gameSettings->write_AB(10,4); gameSettings->write_AB(10,16); gameSettings->write_AB(10,10);
            break;
        }
        case 8:
        {
            gameSettings->write_AB(4,4); gameSettings->write_AB(16,16); gameSettings->write_AB(4,16); gameSettings->write_AB(16,4);
            gameSettings->write_AB(10,4); gameSettings->write_AB(10,16); gameSettings->write_AB(16,10); gameSettings->write_AB(4,10);
            break;
        }
        case 9:
        {
            gameSettings->write_AB(4,4); gameSettings->write_AB(16,16); gameSettings->write_AB(4,16); gameSettings->write_AB(16,4);
            gameSettings->write_AB(10,4); gameSettings->write_AB(10,16); gameSettings->write_AB(16,10); gameSettings->write_AB(4,10);
            gameSettings->write_AB(10,10);
            break;
        }
        default: break;
    }


    //最后传出信号给内部进行操作
    emit changeHandicaps(gameSettings->get_HA());
}
